$(function () {
  let brandtitleid = location.search.split('&')[0].split('=')[1];
  let txt = location.search.split('&')[2].split('=')[1];
  txt = decodeURI(txt).split('十大品牌')[0];
  console.log(txt);
  $('#category').text(txt);
  $('.good').text(txt + '哪个牌子好');
  $('.rank').text(txt + '销量排行');
  $('.rank').text(txt + '销量排行');
  $.ajax({
    url: "http://localhost:3000/api/getbrand",
    data: { brandtitleid: brandtitleid },
    success: function (res) {
      res.txt = txt;
      $('.cate-one').html(template('tmp', res))
    }
  });

  let pagesize = 6; //默认6个数据
  function renderRank() {
    let count = $('.cate-rank').children().length || 0;
    $.ajax({
      url: "http://localhost:3000/api/getbrandproductlist",
      data: {
        brandtitleid: brandtitleid,
        pagesize: pagesize
      },
      success: function (res) {
        if (res.result.list.length == 0) {
          $('.cate-rank').html('<li style="font-size: .6rem; padding-left: .8rem; color: #666; border: none">暂无数据</li>');
          return false;
        }
        end = res.result.list.length < count + 6 ? res.result.list.length : count + 6;
        res.result.end = end;
        res.result.start = count;

        if(res.result.list.length < count){
          return false;
        }
        count = end;

        // console.log(res);
        $('.cate-rank').append(template('tmp2', res.result));
        flag = false;
      }
    });
  }

  let flag = false;

  renderRank();


  $(window).scroll(function () {

    if (flag) {
      return;
    }
    if ($(window).scrollTop() > $('.commit').offset().top - $(window).height() + 40) {
      // console.log(1);
      flag = true;
      renderRank();
    }
  })
})